<?php
namespace upload;

class Upload{
    private $filepath = ''; //上传目录
    private $tmpPath;  //PHP文件临时目录
    private $blobNum; //第几个文件块
    private $totalBlobNum; //文件块总数
    private $fileName; //文件名

    public function __construct($tmpPath,$blobNum,$totalBlobNum,$fileName){
        $this->tmpPath =  $tmpPath;
        $this->blobNum =  $blobNum;
        $this->totalBlobNum =  $totalBlobNum;
        $this->fileName =  $fileName;
        $this->moveFile();
        $this->fileMerge();
    }

    //判断是否是最后一块，如果是则进行文件合成并且删除文件块
    private function fileMerge(){
        if($this->blobNum == $this->totalBlobNum){
            if(!file_exists(ROOT_PATH . $this->filepath.'/'. $this->fileName)){
                for($i=1; $i<= $this->totalBlobNum; $i++){
                    //文件是否存在的判定 弄清楚
                    if(!file_exists(ROOT_PATH . $this->filepath.'/'. $this->fileName.'__'.$i)){
                        exit(encode(['code'=>0,'msg'=>'文件缺失，请重新上传！']));
                    }
                }
                for($i=1; $i<= $this->totalBlobNum; $i++){
                    $blob = file_get_contents(ROOT_PATH . $this->filepath.'/'. $this->fileName.'__'.$i);
                    file_put_contents(ROOT_PATH . $this->filepath.'/'. $this->fileName,$blob,FILE_APPEND);
                    unset($blob);
                    if($i==$this->totalBlobNum){
                        $this->deleteFileBlob();
                    }
                }
            }
        }
    }

    //删除文件块
    private function deleteFileBlob(){
        for($i=1; $i<= $this->totalBlobNum; $i++){
            @unlink(ROOT_PATH . $this->filepath.'/'. $this->fileName.'__'.$i);
        }
    }

    //移动文件
    private function moveFile(){
        $year = date('Y',time());
        $month = date('m', time());  //创建目录
        $new_file =  '/public/upload' . '/'  . $year . '/' . $month;
        $this->filepath = $new_file;
        $this->touchDir();
        $filename = $this->filepath.'/'. $this->fileName.'__'.$this->blobNum;
        if(!file_exists(ROOT_PATH . $this->filepath . '/' . $filename)
         &&!file_exists(ROOT_PATH . $this->filepath.'/'. $this->fileName)){
            move_uploaded_file($this->tmpPath,ROOT_PATH .$filename);
        }
    }

    //API返回数据
    public function apiReturn(){
        if($this->blobNum == $this->totalBlobNum){
            if(file_exists(ROOT_PATH . $this->filepath.'/'. $this->fileName)){
                $data['code'] = 2;
                $data['msg'] = '上传成功！';
                $data['file_path'] = str_replace('.','',$this->filepath).'/'. $this->fileName;
            }
            else{
                $data['code'] = 0;
                $data['msg'] = '上传失败！';
                $data['file_path'] = '';
            }
        }else{
            if(file_exists(ROOT_PATH . $this->filepath . '/' . $this->fileName )){
                $data['code'] = 2;
                $data['msg'] = '上传成功！';
                $data['status']  = 'exist';
                $data['file_path'] = str_replace('.','',$this->filepath).'/'. $this->fileName;
            }
            else if(file_exists(ROOT_PATH . $this->filepath.'/'. $this->fileName.'__'.$this->blobNum)){
                $data['code'] = 1;
                $data['msg'] = '分片上传成功:' . $this->blobNum;
                $data['file_path'] = '';
            }
            else{
                $data['code'] = 0;
                $data['msg'] = '分片上传失败:'. $this->blobNum;
                $data['file_path'] = '';
            }
        }
        return $data;
    }

    //建立上传文件夹
    private function touchDir(){
        if(!file_exists(ROOT_PATH . $this->filepath)){
            mkdir(ROOT_PATH .$this->filepath,0777,true);
        }
    }


}

